package com.halflife.comeElectricity.admin.service;

import com.halflife.framework.domain.BaseDomain;
import com.halflife.framework.participant.user.UserView;
import com.halflife.framework.participant.user.UserViewRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminService {

    @Resource
    UserViewRepository userViewRepository;

    public UserView validUserPassword(String username, String password, Integer type) throws Exception{

        UserView user_ = new UserView();
        //查询用户
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("userName", username);
        if (type != null) {
            queryMap.put("type", type.toString());
        }

        List<UserView> userView = userViewRepository.selectAll(queryMap);
        if(userView.size() > 0){

            if(userView.size() >1)

                user_.setValidMessage("用户不唯一!");
            else{

                UserView user = userView.get(0);
                if (user.getValid()==null||user.getValid() == false) {
                    user_.setValidMessage("user Deactivate!");
                    return user_;
                }
                //验证用户名密码
                if(user.getPassword().equals(password)){

                    //验证通过 推送用户
                    return user;
                }else
                    user_.setValidMessage("userName or passord error");
            }
        }else
            user_.setValidMessage("user is not exist!");

        return user_;
    }

    public String getUrlByType(Integer type) {
        if (type == null) {
            type = -1;
        }
        String url = new String();
        switch (type){

            case 0 : url =  "login_teacher"; break;
            case 1 : url =  "login_student"; break;
            case 2 : url =  "login_staff"; break;
            case 3 : url =  "login"; break;
            default:url = "login";
        }
        return url;
    }


    public String getIndexUrlByType(Integer type) {
        if (type == null) {
            type = -1;
        }
        String url = new String();
        switch (type){

            case 0 : url =  "index"; break;
            case 1 : url =  "assessment/mylist_student"; break;
            case 2 : url =  "index"; break;
            case 3 : url =  "index"; break;
            default:
                url = "index";
        }
        return url;
    }

    /**
     * 获取用户ID
     * @param type
     * @param userViewId
     *
     */
    public String getUserIdByType(Integer type, Integer userViewId) {

        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("valid", true);
        queryMap.put("userViewId", userViewId);
        return null;
    }

    /**
     * 根据类别获取Class
     * @param type
     * @return
     */
    public Class<? extends BaseDomain> getClassByType(Integer type) {

            return null;
    }
}
